/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dao;

import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import model.Authentification;
import model.Compte;

/**
 *
 * @author Jawad
 */
@Stateless
public class AuthentificationImpl implements RemoteAuthentification {

    @PersistenceContext(unitName = "WebSerAuthPU")
    EntityManager em;

    @Override
    public boolean authentifier(String login, String password) {
        Query query = em.createQuery("select a from Authentification a where a.login = :x and a.password = :y ");
        query.setParameter("x", login);
        query.setParameter("y", password);
        List<Authentification> list = query.getResultList();
        if (list.isEmpty()) {
            return false;
        } else {
            return true;
        }
    }

    @Override
    public Compte getCompte(Long numCompte) {
        Query query = em.createQuery("select c from Compte c where c.numCompte = :x");
        query.setParameter("x", numCompte);
        List<Compte> list = query.getResultList();
        if (list.isEmpty()) {
            return new Compte(0L, "unknown", "unknown", 0L);
        } else {
            return list.get(0);
        }
    }
}
